<?php


namespace app\reception\controller;


use app\common\model\PopupApp;
use think\Db;

class Download extends Common
{
    protected $middleware = [
        'VerifyAuth' => ['except' => []],
    ];

    /**
     * app下载规则
     * @return \think\response\Json
     */
    public function app()
    {
        //不是白名单 && （没有归属 || 黑名单 || 审核人员）
        if (!$this->isWhite && (!$this->user->channel_id || $this->isBlack || $this->user->auditor)) {
            return errJson('no');
        }
        $user = $this->user;
        /**快应用*/
        $where[] = ['status', '=', 1];
        $where[] = ['', 'exp', Db::raw("FIND_IN_SET($this->quickAppId,quick_apps)")];
        /**用户群体*/
        $map1[] = ['test_user_id', '=', $user->id];
        if (!$this->isWhite) {
            if (ipUnblocked($this->user)) {
                $map2[] = ['user_grep', 'in', [1, 0]];
            } else {
                $map2[] = ['user_grep', 'in', [2, 0]];
            }
        } else {
            $map2[] = ['user_grep', 'in', [1, 0]];
        }

        /**充值条件*/
        $pur = Db::table("quick_pay_user_record")->where("uid",$user->id)->field("id,pay_money,pay_num")->find();
        if ($pur) {
            $map2[] = ['is_pay', 'in', [0, 1]];
        } else {
            $map2[] = ['is_pay', 'in', [0, 2]];
        }


        $query = $list = PopupApp::where($where)->where(function ($query) use ($map1, $map2) {
            $query->whereOr([$map1, $map2]);
        });
        $list = $query->order("wgt desc,id desc")->select();
//        echo QuickOpen::getLastSql();die;
        $rule = null;
        $now = time();
        foreach ($list as $k => $v) {
            if ($v['is_pay'] == 1) {
                if (!$pur){
                    continue;
                }elseif ($pur['pay_money'] < $v['mini_pay_money']){
                    continue;
                }
            }
            if ($v['register_type'] == 1) {
                $regtime = strtotime($user->regtime);
                $startTime = 0;
                $endTime = INF;
                if ($v['register_start']) {
                    $startTime = $regtime + ($v['register_start'] * 86400);
                }
                if ($v['register_end']) {
                    $endTime = $regtime + ($v['register_end'] * 86400);
                }
                if ($now < $startTime || $now > $endTime) {
                    continue;
                }
            }
            $rule = $v;
            break;
        }
        if ($rule) {
            $data['id'] = $rule->id;
            $data['file_url'] = $rule->file_url;
            $data['pic'] = config('img_domain') . $rule->pic;
            $record = Db::table("quick_popup_app_user")->where("app_id", $rule->id)->where("uid", $this->uid)->find();
            $data['install'] = $record ? 0 : 1;
            return sucJson($data);
        } else {
            return errJson('no');
        }
    }

    /**
     * 添加下载记录
     * @return \think\response\Json
     */
    public function appRecord()
    {
        $params = $this->request->param();
        $result = $this->validate($params, 'app\reception\validate\DownloadValidate.php.appRecord');
        if (true !== $result) {
            return errJson($result);
        }
        $appId = input('app_id');
        $record = Db::table("quick_popup_app_user")->where("app_id", $appId)->where("uid", $this->uid)->find();
        if (!$record) {
            $insert = Db::table("quick_popup_app_user")->insert([
                'app_id' => $appId,
                'uid' => $this->uid
            ]);
            if ($insert) {
                PopupApp::where("id", $appId)->setInc('suc_count');
            }
        }
        return sucJson();
    }
}